package com.sy.simpleegg.platform.kkb.network.base

import com.alibaba.fastjson.JSON
import com.blankj.utilcode.util.LogUtils
import com.ych.ytdevicesdk.config.Constants
import com.ych.ytdevicesdk.utils.SignatureUtils
import java.util.Locale
import java.util.TreeMap

/**
 * @author sy
 * @date 2024/10/11 16:31
 * @desc
 */
abstract class BaseLoader {
    var useTestUrl = false

    fun BaseLoader() {}

    fun getParameter(map: Map<String?, Any?>): Map<String?, Any?> {
        return map
    }

    fun getHeaderRegister(map: Map<String, Any>): Map<String?, Any?>? {
        return this.getHeader(map, "3A5B6BF5FAB891783A5B6CF4FAF79275")
    }

    fun getHeaderYZHRegister(map: Map<String?, Any?>): Map<String?, Any?>? {
        return this.getYZYHeader(map, "3A5B6BF5FAB891783A5B6CF4FAF79275")
    }

    fun getHeader(map: Map<String, Any>): Map<String?, Any?>? {
        return this.getHeader(map, Constants.SIGN_KEY)
    }

    fun getYZYHeader(map: Map<String?, Any?>): Map<String?, Any?>? {
        return this.getYZYHeader(map, Constants.SIGN_KEY)
    }

    fun getHeaderToken(token: String?): Map<String?, Any?>? {
        val headerMap: MutableMap<String?, Any?> = TreeMap<String?, Any?>()
        headerMap["Authorization"] = token
        return headerMap
    }

    fun getHeaderToken(): Map<String?, Any?>? {
        val headerMap: MutableMap<String?, Any?> = TreeMap<String?, Any?>()
        headerMap["Authorization"] = Constants.Token
        return headerMap
    }

    private fun getHeader(map: Map<String, Any>, key: String): Map<String?, Any?>? {
        val sige = StringBuffer()
        val headerMapTyep: MutableMap<String?, Any?> = TreeMap<String?, Any?>()
        var iterator: Iterator<*> = map.keys.iterator()
        while (iterator.hasNext()) {
            val s = iterator.next() as String
            headerMapTyep[s.lowercase(Locale.getDefault())] = map[s]
        }
        LogUtils.d(*arrayOf<Any>("OkHttp: $map"))
        iterator = headerMapTyep.entries.iterator()
        while (iterator.hasNext()) {
            val (key1, value) = iterator.next()
            sige.append((key1 as String).lowercase(Locale.getDefault())).append("=").append(
                value
            )
            if (iterator.hasNext()) {
                sige.append("&")
            }
        }
        LogUtils.d(*arrayOf<Any>("OkHttp: $sige"))
        val headerMap: MutableMap<String?, Any?> = TreeMap<String?, Any?>()
        val genHMAC = SignatureUtils.getSignature(sige.toString(), key)
        if (genHMAC != null) {
            headerMap["Authorization"] = genHMAC
        }
        return headerMap
    }

    private fun getYZYHeader(map: Map<String?, Any?>, key: String): Map<String?, Any?>? {
        val headerMap: MutableMap<String?, Any?> = TreeMap<String?, Any?>()
        val genHMAC = SignatureUtils.getSignature(JSON.toJSONString(map), key)
        if (genHMAC != null) {
            headerMap["Authorization"] = genHMAC
        }
        return headerMap
    }

    fun setIsDebug(useTestUrl: Boolean) {
        this.useTestUrl = useTestUrl
    }
}